package com.xintonghua.bussiness.ui.home;

import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.CollegeAdapter;
import com.xintonghua.bussiness.adapter.MessageeAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CollectResponse;
import com.xintonghua.bussiness.bean.MessageBean;
import com.xintonghua.bussiness.bean.MessageResponse;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/4/1.
 * 包名：com.xintonghua.bussiness.ui.home
 */

public class MessageActivity extends BaseActivity {
    @BindView(R.id.lv_message)
    XRecyclerView lvMessage;
    private int page = 1;
    private List<MessageBean> list = new ArrayList<>();
    private MessageResponse response;
    MessageeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("消息列表");
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        RefreshUtils.initList(this, lvMessage, MyUtils.dip2px(this, 2), R.color.grey_bg);
        adapter = new MessageeAdapter(this, list);
        lvMessage.setAdapter(adapter);
        lvMessage.setLoadingListener(this);
        find();
    }

    private void find() {
        httpCent.message(page, this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    list.clear();
                }
                response = JSONObject.parseObject((String) data, MessageResponse.class);
                list.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvMessage);
                httpCent.updatePingtai(this, MyState.requestTwo);
                break;
            case MyState.requestTwo:

                break;

        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvMessage)) {
            page++;
            find();
        }
    }
}
